package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.entity.*;
import com.ycxy.sh.transaction.dao.vo.UserInfoAvatarVo;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.*;
import com.ycxy.sh.transaction.service.*;
import com.ycxy.sh.transaction.util.KeyUtil;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 宋H
 * @Date: 2023/3/27
 * @Description: com.ycxy.sh.transaction.service
 * @version: 1.0
 */
@Slf4j
@Service
public class CommentReplyServiceImpl implements CommentReplyService {

    @Autowired
    private CommodityMapper commodityMapperl;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private NoticesMapper noticesMapper;

    /**
     * 查询商品下的评论和回复
     * @param commentid
     */
    @Override
    public List<Comment> selectCommentReply(Long commentid) {
        log.debug("开始处理查询商品下的评论和回复的业务...");
        /**查询评论*/
        List<Comment> commentsList = commentMapper.selectCommentsById(commentid);
        for (Comment comment : commentsList) {
            /**查询对应评论下的回复*/
            List<Reply> repliesList = replyMapper.selectReplys(comment.getCommentId());
            for (Reply reply : repliesList) {
                /**查询回复者的昵称和头像信息*/
                UserInfoAvatarVo ruser = userInfoMapper.selectPartInfo(reply.getReplyUserId());
                /**查询被回复者的昵称信息*/
                UserInfoAvatarVo cuser = userInfoMapper.selectPartInfo(reply.getCommentUserId());
                /**添加回复中涉及到的用户昵称及头像信息*/
                reply.setRusername(ruser.getUsername()).setRuimage(ruser.getUserImage()).setCusername(cuser.getUsername());
            }
            /**查询评论者的昵称和头像信息*/
            System.out.println("评论者ID：" + comment.getCommentUserId());
            UserInfoAvatarVo userInfo = userInfoMapper.selectPartInfo(comment.getCommentUserId());
            /**添加评论下的回复及评论者昵称和头像信息*/
            comment.setReplyLsit(repliesList).setCusername(userInfo.getUsername()).setCuImage(userInfo.getUserImage());
        }
        if(commentsList.isEmpty()){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"查询评论回复失败");
        }
       return commentsList;
    }

    /**
     * 用户回复评论
     * @param reply
     * @param replyuserid
     */
    @Override
    public void replyComment(Reply reply, Long replyuserid) {
        log.debug("开始处理用户回复评论业务...");
        //获取回复内容
        String replyContent = reply.getReplyContent();

        if (StringUtils.isEmpty(replyuserid)) {
            String message="请登录后再评论";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_FORBIDDEN,message);
        }

        replyContent = replyContent.replace("<", "&lt;");
        replyContent = replyContent.replace(">", "&gt;");
        replyContent = replyContent.replace("'", "\"");

        reply.setReplyId(Long.valueOf(KeyUtil.genUniqueKey())).setReplyUserId(replyuserid).setReplyContent(replyContent);
        //插入回复
        int row = replyMapper.inset(reply);
        if (row == 1){
            /**发出评论回复通知消息*/
            Commodity commodity = commodityMapperl.selectById(new Commodity().setCommodityId(reply.getCommodityId()));
            Notices notices = new Notices().setNoticeId(Long.valueOf(KeyUtil.genUniqueKey()))
                    .setUserId(reply.getCommentUserId()).setNoticeType("评论回复")
                    .setNoticeContent("有小伙伴在 <a href=/product-detail/"+reply.getCommentId()+" style=\"color:#08bf91\" target=\"_blank\" >"+commodity.getCommodityName()+"</a> 下回复了您的评论，快去看看吧。");
            //插入通知
            int insert = noticesMapper.insert(notices);
            if(insert!=1){
                String message="插入通知信息失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_INSERT,message);
            }
        }
        String msg="回复失败";
        log.debug(msg);
        throw new ServiceException(ServiceCode.ERR_NOT_FOUND,msg);
    }

    /**
     * 根据评论者id和商品发布者id删除评论
     * @param commentuserid
     * @param commentid
     */
    @Override
    public void deleteComment(Long commentid,Long commentuserid) {
        //查询评论中用户信息
        Comment comment = commentMapper.selectCommentUserInfoById(commentid);
        /**如果是评论者本人或者商品发布者则进行删除操作*/
        if (comment.getCommentUserId().equals(commentuserid) || comment.getCommodityPublishUserId().equals(commentid)){
            int i = commentMapper.delete(commentid);
            int j = replyMapper.delete(new Reply().setCommentId(commentid));
            if (i != 1 || j != 1){
                String message="评论删除失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_DELETE,message);
            }
               log.debug("评论删除成功");
        }else{
            String message1="操作失败，可能是因为您的权限不够!";
            log.debug(message1);
            throw new ServiceException(ServiceCode.ERR_FORBIDDEN,message1);
        }
    }
    /**
     * 根据回复者id和商品发布者id删除回复
     * @param replyid
     * @param replyuserid
     */
    @Override
    public void deleteReply(Long replyid, Long replyuserid) {
        log.debug("开始执行删除回复信息的业务...");
        Reply reply = replyMapper.selectReplyUserInfoById(replyid);
        /**如果是回复者本人或者商品发布者则进行删除操作*/
        if (reply.getReplyUserId().equals(replyuserid) || reply.getReplyContent().equals(replyuserid)){
            //删除回复
            Integer i = replyMapper.delete(new Reply().setReplyId(replyid));
            if (i != 1){
                String message="回复删除失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_DELETE,message);
            }
            log.debug("回复删除成功");
        }else{
            String message1="操作失败，可能是因为您的权限不够!";
            log.debug(message1);
            throw new ServiceException(ServiceCode.ERR_FORBIDDEN,message1);
        }
    }
}
